import 'dart:io';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:palyer/app/modules/drop/drop/controller.dart';

class DropPage extends StatelessWidget {
  const DropPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(DropController());

    return Scaffold(
      appBar: AppBar(
        title: const Text('文件拖动'),
      ),
      body: GetBuilder<DropController>(
        builder: (_) {
          return DropTarget(
            onDragDone: (detail) {
                controller.list.addAll(detail.files);
                controller.update();
            },
            onDragEntered: (detail) {
                controller.dragging = true;
                controller.update();
            },
            onDragExited: (detail) {
                controller.dragging = false;
                controller.update();
            },
            child: Container(
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              color: controller.dragging ? Colors.blue.withOpacity(0.4) : Colors.black26,
              child: controller.list.isEmpty
                  ? const Center(child: Text('Drop here'))
                  : ListView.builder(
                      itemCount: controller.list.length,
                      itemBuilder: (context, index) {
                        final file = controller.list[index];
                        return Stack(
                          children: [
                            Positioned(
                                child: Image.file(File(file.path))
                            ),
                            ListTile(
                              title: Text(file.path),
                              subtitle: Text(file.name.toString()),
                              trailing: IconButton(
                                icon: const Icon(Icons.delete),
                                onPressed: () {
                                  controller.list.removeAt(index);
                                  controller.update();
                                },
                              ),
                            ),
                          ],
                        );
                      },
                    )
            ),
          );
        },
      ),
    );
  }

}
